package com.mademoisellegeek.carrentalsimulation.txt;

import com.mademoisellegeek.carrentalsimulation.CarRentalException;
import com.mademoisellegeek.carrentalsimulation.enums.CarRentalExceptionType;
import com.mademoisellegeek.carrentalsimulation.CarRentalSimulationApp;
import com.mademoisellegeek.carrentalsimulation.Garage;
import com.mademoisellegeek.carrentalsimulation.OptimisationData;
import com.mademoisellegeek.carrentalsimulation.Travel;
import com.mademoisellegeek.carrentalsimulation.Vehicle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DocumentParser {

    public static OptimisationData populateGarageDictionary(OptimisationData data, String fileName) throws CarRentalException {

        File file = new File(fileName);
        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] parsedLine = line.split(",");
                int garageId = Integer.parseInt(parsedLine[0]);
                int initialNbParkedVehicles = getNbVehiclesParked(data.getVehicleDictionary(), garageId, 1);
                data.addGarage(garageId, new Garage(garageId,
                        parsedLine[1],
                        Integer.parseInt(parsedLine[2]),
                        initialNbParkedVehicles));
            }
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            throw new CarRentalException(CarRentalSimulationApp.GARAGES, CarRentalExceptionType.FILE_PARSER);
        }
        return data;
    }

    public static ArrayList<Travel> getRequestList(String fileName) throws CarRentalException {

        ArrayList<Travel> requestList = new ArrayList<Travel>();
        File file = new File(fileName);
        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] parsedLine = line.split(",");
                requestList.add(new Travel(Integer.parseInt((parsedLine[0])),
                        parsedLine[1],
                        parsedLine[2],
                        Integer.parseInt(parsedLine[3]),
                        Integer.parseInt(parsedLine[4])));
            }
            if (reader != null) {
                reader.close();
            }
            return requestList;
        } catch (Exception e) {
            throw new CarRentalException(CarRentalSimulationApp.REQUESTS, CarRentalExceptionType.FILE_PARSER);
        }
    }

    public static OptimisationData populateVehicleDictionary(OptimisationData data, String fileName) throws CarRentalException {

        File file = new File(fileName);
        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] parsedLine = line.split(",");
                int vehicleId = Integer.parseInt(parsedLine[0]);
                int garageId = Integer.parseInt(parsedLine[1]);
                data.addVehicle(vehicleId, new Vehicle(vehicleId, garageId));
            }
            if (reader != null) {
                reader.close();
            }
            return data;
        } catch (Exception e) {
            throw new CarRentalException(CarRentalSimulationApp.VEHICLES, CarRentalExceptionType.FILE_PARSER);
        }
    }

    public static Map<String, Integer> getTownList(String fileName) throws CarRentalException {

        File file = new File(fileName);
        BufferedReader reader;
        Map<String, Integer> townList = new HashMap<String, Integer>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] parsedLine = line.split(",");
                townList.put(parsedLine[1],
                        Integer.parseInt((parsedLine[0])));
            }
            if (reader != null) {
                reader.close();
            }
            return townList;
        } catch (Exception e) {
            throw new CarRentalException(CarRentalSimulationApp.TOWNS, CarRentalExceptionType.FILE_PARSER);
        }
    }

    public static int[][] getDistanceList(Map<String, Integer> towns, String fileName) throws CarRentalException {

        File file = new File(fileName);
        BufferedReader reader;
        int[][] distances = new int[towns.size()][towns.size()];

        try {
            reader = new BufferedReader(new FileReader(file));
            String line = reader.readLine();
            while (line != null) {
                String[] parsedLine = line.split(",");
                int town1 = towns.get(parsedLine[0]);
                int town2 = towns.get(parsedLine[1]);
                //We consider that in one day a car can drive 600 km
                int nbDaysDistance = Integer.parseInt((parsedLine[2])) / 600 + 1;
                distances[town1 - 1][town2 - 1] = nbDaysDistance;
                distances[town2 - 1][town1 - 1] = nbDaysDistance;
                line = reader.readLine();
            }
            if (reader != null) {
                reader.close();
            }
            return distances;
        } catch (Exception e) {
            throw new CarRentalException(CarRentalSimulationApp.DISTANCES, CarRentalExceptionType.FILE_PARSER);
        }
    }

    private static int getNbVehiclesParked(Map<Integer, Vehicle> carDictionary, int garageId, int date) {
        int result = 0;
        for (Vehicle vehicle : carDictionary.values()) {
            if (vehicle.getGarageIdForDay(date) == garageId) {
                result++;
            }
        }
        return result;
    }
}